//
//  BONaviViewController.m
//

#import "UIBarButtonItem+Extension.h"
#import "BONaviViewController.h"


@interface BONaviViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BONaviViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
}

+ (void)initialize
{
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
//    [[UINavigationBar appearance] setBarTintColor:LGCOLOR(@"#FF9F08")];
    [[UINavigationBar appearance] setTitleTextAttributes:textAttrs];
    [UINavigationBar appearance].translucent = NO;
    [UINavigationBar appearance].shadowImage = [UIImage new];
}



- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) { // 这时push进来的控制器viewController，不是第一个子控制器（不是根控制器）
        /* 自动显示和隐藏tabbar */
        viewController.hidesBottomBarWhenPushed = YES;

        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"ic_return_black" highImage:@"ic_return_black"];

        // 侧滑回退
        self.interactivePopGestureRecognizer.delegate = (id)self;
    }

    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    // 因为self本来就是一个导航控制器，self.navigationController这里是nil的
    [self popViewControllerAnimated:YES];
}


//解决根页面手势问题
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ( gestureRecognizer == self.interactivePopGestureRecognizer )
       {
           UIViewController *vc = self.viewControllers.lastObject;
          //  禁用某些不支持侧滑返回的页面
//           if ([vc isKindOfClass:[OpenContainerDoorVC class]]) {
//               return NO;
//           }
         //  禁用根目录的侧滑手势
           if ( self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] )
           {
               return NO;
           }
       }
       return YES;
}

//Bug描述：当当前页面最底层是scrollView时，侧滑手势失效问题。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        if (gestureRecognizer.state != UIGestureRecognizerStatePossible) {
            return YES;
        }
    }
    return NO;
}

@end
